<?php
/**
 * Created by PhpStorm.
 * User: lihao
 * Date: 2017/7/20
 * Time: 下午5:23
 */

class Photo_model extends CI_Model {
    public function __construct() {
        parent::__construct();
    }

    public function createDir($dir) {
        $result = $this->db->insert('pics',array('name'=>$dir));
        return $result;
    }

    public function getDir() {
        $result = $this->db->get('pics')->result_array();
        return $result;
    }

    public function getPhotots($picid, $limit) {
        if ($limit != null) {
            $this->db->limit(1);
        }
        if ($picid != null) {
            $this->db->where('picid', $picid);
        }
        $this->db->order_by('imgid', 'DESC');
        $result = $this->db->get('images')->result_array();
        return $result;
    }

    public function saveImg($name, $uid = 100000, $picid = 0) {
        $arr = array(
            'name'=>$name,
            'uid'=>$uid,
            'picid'=>$picid
        );
        $result = $this->db->insert('images', $arr);
        return $result;
    }

//    方法整合
//    获取所有图片文件夹
    public function getDirsWithCover() {
        $result = $this->getDir();
        $arr = array();
        foreach ($result as $item) {
            $re = $this->getPhotots($item['picid'], 1);
            $item['cover'] = count($re) > 0 ? $re[0]['name'] : "";
            $arr[] = $item;
        }
        return $arr;
    }

}